package jupitermouse.site.juc.demo.aqs.cyclibarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * <p>
 * 模拟数据导入操作
 * </p>
 *
 * @author jupiter
 */
public class DataImportThread extends Thread {

    private CyclicBarrier cyclicBarrier;

    private String path;

    private int time;

    public DataImportThread(CyclicBarrier cyclicBarrier, String path) {
        this.cyclicBarrier = cyclicBarrier;
        this.path = path;
    }

    public DataImportThread(CyclicBarrier cyclicBarrier, String path, int time) {
        this.cyclicBarrier = cyclicBarrier;
        this.path = path;
        this.time = time;
    }

    @Override
    public void run() {
        System.out.println("start import:" + path + "data");
        try {
            Thread.sleep(time);
            System.out.println("import success" + path);
            cyclicBarrier.await(10, TimeUnit.SECONDS);// 类似 Condition.await
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException | TimeoutException e) {
            e.printStackTrace();
        }
    }


}
